//
//  GuideView.m
//  aMoment
//
//  Created by dllo on 15/10/22.
//  Copyright © 2015年 LuckyHan. All rights reserved.
//

#import "GuideView.h"
#import "RedefineHeader.h"

@implementation GuideView

- (void)dealloc
{
    [_scrollView release];
    [_imageArray release];
    [_enterButton release];
    [_skipButton release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //图片数组
        self.imageArray = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
        
        //滚动视图
        self.scrollView = [[UIScrollView alloc] init];
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.bounces = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:_scrollView];
        [_scrollView release];
        
        //进入按钮
        self.enterButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _enterButton.alpha = 0;
        [_enterButton setTitle:@"点击进入轻享之旅" forState:UIControlStateNormal];
        _enterButton.layer.borderWidth = 1;
        _enterButton.layer.cornerRadius = 5;
        _enterButton.layer.borderColor = [UIColor whiteColor].CGColor;
        [_enterButton addTarget:self action:@selector(removeGuideView) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_enterButton];
        [_enterButton release];
        
        //跳过按钮
        self.skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_skipButton setTitle:@"跳过" forState:UIControlStateNormal];
        _skipButton.layer.borderWidth = 1;
        _skipButton.layer.cornerRadius = 5;
        _skipButton.layer.borderColor = [UIColor whiteColor].CGColor;
        [_skipButton addTarget:self action:@selector(removeGuideView) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_skipButton];
        [_skipButton release];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _scrollView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * _imageArray.count, SCREEN_HEIGHT);
    for (NSInteger i = 0; i < _imageArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        imageView.image = [UIImage imageNamed:_imageArray[i]];
        [_scrollView addSubview:imageView];
        [imageView release];
    }
    
    //进入按钮
    _enterButton.frame = CGRectMake(100, SCREEN_HEIGHT - 100, 175, 40);
    _enterButton.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT - 80);
    
    //跳过按钮
    _skipButton.frame = CGRectMake(SCREEN_WIDTH - 100, 70, 70, 30);
}

#pragma mark 滚动中
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x > SCREEN_WIDTH * (_imageArray.count - 1) - 1) {
        [UIView animateWithDuration:.5 animations:^{
            _skipButton.alpha = 0;
            _enterButton.alpha = 1;
        }];
    }
    else {
        [UIView animateWithDuration:.5 animations:^{
            _skipButton.alpha = 1;
            _enterButton.alpha = 0;
        }];
    }
}

#pragma mark 移除引导页
- (void)removeGuideView
{
    [UIView animateWithDuration:2.0 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self removeFromSuperview];
        });
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
